import 'package:core_package/core_package.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/model/take_save_model.dart';
import 'package:flutter_app_unionad/ui/basice/title_alert_widget.dart';
import 'package:flutter_app_unionad/ui/basice/withdraw_deposit_widget.dart';
import 'package:flutter_app_unionad/ui/promotion/delegate/account_amount_delegate.dart';
import 'package:flutter_app_unionad/ui/withdraw_deposit/withdraw_deposit_page.dart';
import 'package:permission_handler/permission_handler.dart';

import 'h5_generate_contract_model.dart';
import 'contract_info_model.dart';
import 'contract_page.dart';
import 'withdraw_deposit_request.dart';

class WithdrawDepositManager {
  static withdrawDeposit(
    BuildContext context,
    AccountAmountDelegate delegate, {
    TaskSaveModel? accountModel,
    //如果参进来代码是
  }) async {
    ProgressDialog.showProgress(context);
    final future = await WithdrawDepositRequest.getContractInfo(type: accountModel == null ? 1 : 0);
    ProgressDialog.dismiss(context);

    if (future == null) {
      ShowToastWidget.showToast(msg: '提现失败');
      return;
    }

    if (future.isContractSign == null || future.isContractSign == false) {
      var status = await Permission.camera.status;
      if (!status.isGranted) {
        status = await Permission.camera.request();
      }

      var status1 = await Permission.photos.status;
      if (!status1.isGranted) {
        status1 = await Permission.photos.request();
      }

      if (!status.isGranted) {
        ShowToastWidget.showToast(msg: "请开启访问相册权限");
        //这里需要开启权限
        return;
      }
      NavigatorUtil.push(context, ContractPage.routeName);
    } else {
      //判断是否能提现
      if (future.isCashTime == false) {
        //不是提现日

        ShowCenterAlertDialog.showAlert(context, height: accountModel == null ? 200 : 170, builder: (ctx) {
          return TitleAlertWidget(
            title: "今天不是提现日",
            subTitle: future.cashLabel,
            actions: ["好的"],
            actionsColors: [MyColors.color_245_174_41],
            actionsTitleColors: [Colors.white],
            onTap: (index) {
              Navigator.pop(context);
            },
          );
        });
        return;
      }
      // //判断是否绑定银行卡
      if (future.isSaveBankInfo == null || future.isSaveBankInfo == false) {
        NavigatorUtil.push(context, WithdrawDepositPage.routeName, arguments: accountModel).then((value) {
          if (value != null && value) {
            //刷新金额
            delegate.reloadAmount();
          }
        });
        return;
      }

      ShowCenterAlertDialog.showAlert(
        context,
        builder: (ctx) {
          return WithdrawDepositWidget(
            title: accountModel == null ? '请输入提现金额' : '请输入提现金币',
            onTap: (value) {
              if (value.length == 0) {
                ShowToastWidget.showToast(msg: accountModel == null ? '请输入提现金额' : '请输入提现金币');
                return;
              }
              if (num.parse(value) < 0) {
                ShowToastWidget.showToast(msg: '提现金额不能小于0');
                return;
              }
              if (accountModel != null) {
                if (num.parse(value) < 10000) {
                  ShowToastWidget.showToast(msg: '提现金币不能小于10000');
                  return;
                }
                if (num.parse(value) > accountModel.userPoint!) {
                  ShowToastWidget.showToast(msg: '可提现金币不能大于${accountModel.userPoint}');
                  return;
                }
              }

              FocusScope.of(context).requestFocus(FocusNode());

              ProgressDialog.showProgress(context);
              WithdrawDepositRequest.withdrawDeposit(amount: num.parse(value), type: accountModel != null ? 0 : 1)
                  .then((value) {
                ProgressDialog.dismiss(context);
                if (value) {
                  delegate.reloadAmount();

                  Navigator.pop(context);
                }
              });
            },
            isBi: accountModel == null ? false : true,
          );
        },
        width: 335.px,
        height: accountModel == null ? 200.px : 250.px,
      );
    }
  }
}
